package com.mv.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mv.mapper.MovieMapper;
import com.mv.pojo.Movie;
import com.mv.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MovieServiceImpl implements MovieService{

    @Autowired
    private MovieMapper movieMapper;

    @Override
    public PageResult getMovieListByPage(PageResult pageResult) {
        Page<Movie> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        page=movieMapper.selectPage(page,null);
        pageResult.setTotal(page.getTotal()).setRows(page.getRecords());
        return pageResult;
    }
    @Transactional
    @Override
    public void addMovie(Movie movie) {
        movie.setCount(0);
        movieMapper.insert(movie);
    }
    @Transactional
    @Override
    public void deleteMovie(Integer id) {
        movieMapper.deleteById(id);
    }
    @Transactional
    @Override
    public void updateMovie(Movie movie) {
        movieMapper.updateById(movie);
    }

    @Override
    public Integer getCount() {
        return movieMapper.getCount();
    }

    @Override
    public Movie getMovie(Integer id) {
        return movieMapper.selectById(id);
    }
    @Transactional
    @Override
    public void addMovieCount(Movie movie) {
        movie.setCount(movie.getCount()+movie.getNum());
        movieMapper.updateById(movie);
    }

    @Override
    public List<Movie> getMovieList() {
        return movieMapper.selectList(null);
    }

}
